<?php
if(!defined('PATH')){die('Access denied.');}
include_once PATH.'template/_head.php';
function saveConfigDefine($cfgfp,$cfgArr,$defExt=''){
	fwrite($cfgfp,'<?php');
	foreach($cfgArr as $k=>$v){
		switch(gettype($v)){
			case 'string':
				$v="'".strtr($v,"'",' ')."'";
				break;
			case 'boolean':
				$v=$v?'TRUE':'FALSE';
				break;
			case 'integer':
				break;
			case 'double':
				break;
			default:
				$v='\'NOT SUPPORTED\'';
				break;
		}
		if(gettype($k)=='string'){
			fwrite($cfgfp,"\ndefine('{$defExt}{$k}',{$v});");
		}
	}
	return TRUE;
}
if(isset($_REQUEST['act']) and $_REQUEST['act']=='done'){
	echo 'Connect Mysql database...<br />';
	@mysql_connect($_REQUEST['dbsrv'],$_REQUEST['dbusr'],$_REQUEST['dbpsw']) or die('Can not connect MySQL server.');
	mysql_select_db($_REQUEST['dbnam']) or die('MySQL database not exists.');
	if(isset($_REQUEST['insoptClean']) and $_REQUEST['insoptClean']=='yes'){
		echo 'Drop SQL tables...<br />';
		mysql_query("DROP TABLE IF EXISTS `pw_users`;");
		mysql_query("DROP TABLE IF EXISTS `pw_piclog`;");
	}
	echo 'Create SQL table...<br />';
	mysql_query("CREATE TABLE IF NOT EXISTS `pw_users`(`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT UNIQUE, `name` CHAR(20) NOT NULL UNIQUE, `psw` CHAR(32) NOT NULL, `hash` CHAR(32) DEFAULT '');");
	mysql_query("CREATE TABLE IF NOT EXISTS `pw_piclog`(`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT UNIQUE, `belong` INT NOT NULL, `rhash` CHAR(32) NOT NULL, `fpath` TEXT NOT NULL);");
	if(isset($_REQUEST['insoptCdefusr']) and $_REQUEST['insoptCdefusr']=='yes'){
		echo 'Add default user...<br />';
		$dbq=mysql_query("SELECT `id` FROM `pw_users` WHERE `name`='DEFAULT';");
		if(mysql_fetch_array($dbq)){
			echo '<b>Warrning:</b> user &quot;DEFAULT&quot; already exists! user creation canceled.<br />';
		}else{
			$defaultUserPsw=md5('DEFAULT');
			mysql_query("INSERT INTO `pw_users`(`name`, `psw`) VALUES('DEFAULT', '{$defaultUserPsw}');");
		}
		mysql_free_result($dbq);
	}
	echo 'Save config file...<br />';
	$cfgfp=@fopen(PATH.'config.php','w') or die('Can not create file config.php');
	$configData=array(
			'MYSQL_SERVER'=>$_REQUEST['dbsrv'],
			'MYSQL_USERNAME'=>$_REQUEST['dbusr'],
			'MYSQL_PASSWORD'=>$_REQUEST['dbpsw'],
			'MYSQL_DBNAME'=>$_REQUEST['dbnam']
		);
	@saveConfigDefine($cfgfp,$configData,'CFG_') or die('Can not write data to file config.php');
	fclose($cfgfp);
	echo 'Install succeed.';
}else{
	echo <<<EOF
		<h3>SPW-Installer</h3>
		<div>
			<p>
				Please fill the information:
				<br />
				<form action="{$_SERVER['PHP_SELF']}" method="post">
					<p>
						MySQL server address (eg. &quot;localhost&quot;, &quot;localhost:3306&quot;):
						<br />
						<input name="dbsrv" type="text" size="16" value="localhost:3306" />
					</p>
					<p>
						MySQL username:
						<br />
						<input name="dbusr" type="text" size="16" value="root" />
					</p>
					<p>
						MySQL password ( \ &quot; &apos; are not allowed in your password):
						<br />
						<input name="dbpsw" type="password" size="16" />
					</p>
					<p>
						MySQL database name (notice: this program will not create any database, please contact your MySQL server provider or use other software to create a database for this program):
						<br />
						<input name="dbnam" type="text" size="16" />
					</p>
					<p>
						<input name="insoptClean" type="checkbox" value="yes" checked="checked" />
						Remove original data in the database.
					</p>
					<p>
						<input name="insoptCdefusr" type="checkbox" value="yes" checked="checked" />
						Create a default user (username: &quot;DEFAULT&quot;, password: &quot;DEFAULT&quot;).
					</p>
					<p>
						<input name="act" type="hidden" value="done" />
						<input type="submit" value="Install" />
					</p>
				</form>
			</p>
		</div>
EOF;
}
include_once PATH.'template/_foot.php';